;(function(undefined) {
    "use strict"
    var _global;

    function result(args,fn){
        var argsArr = Array.prototype.slice.call(args);
        if(argsArr.length > 0){
            return argsArr.reduce(fn);
        } else {
            return 0;
        }
    }
    var  rightsList = {
        userColumnsInit:function(){
            return  [
	            {
	            	checkbox: true
	            },
	            {
	            	title: '用户姓名',
	                field: 'userName',
	                width: '100px',
	                sortable : true
	            },
	            {
	            	title: '登录账号',
	                field: 'userCode',
	                sortable : true
	            }
            ]
        },
        selectUserColumnsInit:function(){
            return  [
	            {
	            	checkbox: true
	            },
	            {
	            	title: '用户姓名',
	                field: 'userName',
	                width: '100px',
	                sortable : true
	            },
	            {
	            	title: '登录账号',
	                field: 'userCode',
	                sortable : true
	            }
            ]
        },
        userGridInit:function(columns){
            return $("#userTable").bootstrapTable({
                url:'',
                columns: columns,
                striped: 'true',
                clickToSelect: true,
                classes: 'table table-hover qxttable table-com roletable',
                height: 305,
                onCheck: function(){
                	changeArrowStatus("userTable");
                },
                onUncheck: function(){
                	changeArrowStatus("userTable");
                },
                onCheckAll: function(){
                	changeArrowStatus("userTable");
                },
                onUncheckAll: function(){
                	changeArrowStatus("userTable");
                }
            });
        },
        selectUserGridInit:function(columns){
            return $("#selectUserTable").bootstrapTable({
                url:'',
                columns: columns,
                striped: 'true',
                clickToSelect: true,
                classes: 'table table-hover qxttable  table2 table-com roletable',
                height: 305,
                onCheck: function(){
                	changeArrowStatus("selectUserTable");
                },
                onUncheck: function(){
                	changeArrowStatus("selectUserTable");
                },
                onCheckAll: function(){
                	changeArrowStatus("selectUserTable");
                },
                onUncheckAll: function(){
                	changeArrowStatus("selectUserTable");
                }
            });
        },
        userDataLoad:function(){
        	$("#userTable").bootstrapTable("removeAll");
        	$("#userTable").bootstrapTable("showLoading");
        	var queryUser = $('#queryUser').val();
        	var roleCode = $('#selectRoleCode').val();
        	var args = {};
        	args.roleCode = roleCode;
        	args.userName = queryUser;
    		args.userCode = queryUser;
            $$.ajax({
		        url:"/rest/BaseUserBusiness/listByCodeOrName",
		        data : args,
		        success : function(data){
		            $("#userTable").bootstrapTable("load",data);
		            changeArrowStatus("userTable");
		            $("#userTable").bootstrapTable("hideLoading");
		        }
		    });
        },
        userSelectDataLoad:function(){
        	$("#selectUserTable").bootstrapTable("removeAll");
        	$("#selectUserTable").bootstrapTable("showLoading");
        	var roleCode = $('#selectRoleCode').val();
        	var querySelectUser = $('#querySelectUser').val();
        	var args = {};
        	args.roleCode = roleCode;
    		args.userCode = querySelectUser;
        	args.userName = querySelectUser;
        	$$.ajax({
		        url:"/rest/BaseUserRoleBusiness/listUserByRoleCode",
		        data : args,
		        success : function(data){
		            $("#selectUserTable").bootstrapTable("load",data);
		            changeArrowStatus("selectUserTable");
		            $("#selectUserTable").bootstrapTable("hideLoading");
		        }
		    });
        }
    }

    // 将插件对象暴露给全局对象
    _global = (function(){ return this || (0, eval)('this'); }());
    if (typeof module !== "undefined" && module.exports) {
        module.exports = rightsList;
    } else if (typeof define === "function" && define.amd) {
        define(function(){return rightsList;});
    } else {
        !('plugin' in _global) && (_global.rightsList = rightsList);
    }
}());

/**
 * 初始化数据
 * @param {Object} roleCode
 */
function initData(roleCode){
	$('#selectRoleCode').val(roleCode);
	rightsList.userDataLoad();
	rightsList.userSelectDataLoad();
}

/**
 * 加载数据后移动数据箭头按钮颜色控制
 * @param {Object} tableId
 */
function changeArrowStatus(tableId){
	//获取选中行
	var selectRowDatas = $("#"+tableId).bootstrapTable('getSelections');
	if(selectRowDatas && selectRowDatas.length>0){
		if("userTable"==tableId){
			$('#addToRight').addClass('click-arrow-on');
		}else if("selectUserTable"==tableId){
			$('#removeRight').addClass('click-arrow-on');
		}
	}else{
		if("userTable"==tableId){
			$('#addToRight').removeClass('click-arrow-on');
		}else if("selectUserTable"==tableId){
			$('#removeRight').removeClass('click-arrow-on');
		}
	}
}

/**
 * 保存数据
 * @param {Object} userCodes
 */
function saveUserCodes(userCodes){
	var result = false;
	var roleCode = $('#selectRoleCode').val();
	var args = {};
	args.roleCode = roleCode;
	args.userCodes = userCodes.join(',');
	$$.ajax({
        url:"/rest/BaseUserRoleBusiness/saveRoleAndUserData",
        async:false,
        data : args,
        success : function(res){
        	result = true;
        },
        error: function(){
        	result = false;
        }
    });
	return result;
}

/**
 * 删除数据
 * @param {Object} userCodes
 */
function batchDeleteRoleUser(userCodes){
	var result = false;
	var roleCode = $('#selectRoleCode').val();
	var args = {};
	args.roleCode = roleCode;
	args.userCodes = userCodes.join(',');
	$$.ajax({
        url:"/rest/BaseUserRoleBusiness/deleteRoleAndUserData",
        async:false,
        data : args,
        success : function(res){
        	result = true;
        },
        error: function(){
        	result = false;
        }
    });
	return result;
}

$(function () {
    //渲染表格
    var userColumns = rightsList.userColumnsInit();
    var userTable = rightsList.userGridInit(userColumns);
    
    var selectUserColumns = rightsList.selectUserColumnsInit();
    var selectUserTable = rightsList.selectUserGridInit(selectUserColumns);
    
    //搜索-user
    $('#searchBtnUser').on('click',function(){
    	rightsList.userDataLoad();
    });
    //回车搜索-user
    $('#queryUser').bind('keypress',function(e){
        if(e.keyCode == 13){
            rightsList.userDataLoad();
        }
    });
    //重置-user
    $('#searchResetUser').on('click',function(){
    	$('#queryUser').val('');
    	rightsList.userDataLoad();
    });
    
    //搜索-selectUser
    $('#searchBtnSelectUser').on('click',function(){
    	rightsList.userSelectDataLoad();
    });
    //回车搜索-selectUser
    $('#querySelectUser').bind('keypress',function(e){
        if(e.keyCode == 13){
            rightsList.userSelectDataLoad();
        }
    });
    //重置-selectUser
    $('#searchResetSelectUser').on('click',function(){
    	$('#querySelectUser').val('');
    	rightsList.userSelectDataLoad();
    });
    
    //添加左侧选中用户到右侧
    $('#addToRight').on('click',function(){
    	//已经选择的用户信息
    	var rowDatas = $("#userTable").bootstrapTable('getSelections');
	    if (rowDatas && rowDatas.length > 0) {
	    	var userCodes = new Array();
	    	for(var i=0;i<rowDatas.length;i++){
	    		var rowData = rowDatas[i];
	    		userCodes.push(rowData.userCode);
	    	}
	    	if(userCodes.length>0){
	    		//保存数据
	    		if(saveUserCodes(userCodes)){
	    			rightsList.userDataLoad();
	    			rightsList.userSelectDataLoad();
	    		}
	    	}
	    }
    });
    
    //删除右侧选中用户
    $('#removeRight').on('click',function(){
    	var selectRowDatas = $("#selectUserTable").bootstrapTable('getSelections');
    	var deleteUserCode = new Array();
    	if(selectRowDatas && selectRowDatas.length>0){
    		for(var i=0;i<selectRowDatas.length;i++){
	    		var rowData = selectRowDatas[i];
	    		deleteUserCode.push(rowData.userCode);
	    	}
    	}
    	if(deleteUserCode.length>0){
    		if(batchDeleteRoleUser(deleteUserCode)){
	    		rightsList.userDataLoad();
				rightsList.userSelectDataLoad();
	    	}else{
	    		
	    	}
    	}
    });
    
})